﻿using MyInterFace;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using XMLHelper;
using ZdjDataModel;

namespace DataController
{
    public class UserController : IController<LoginModel>,IDisposable
    {
        #region 字段
        private readonly string dir = Path.GetFullPath(".") + @"\xml\login\users.xml";
        private static readonly string dirNow = Path.GetFullPath(".") + @"\xml\login\user.xml";
        private readonly string startUpPath = Path.GetFullPath(".") + @"\";
        private const string loginOK = "欢迎登陆！\n";
        private const string loginErro = "登陆失败！\n";
        private const string notEmptyNameOrPaw = "用户名或者密码为空！\n";
        private const string erroeNameOrPaw = "用户名或者密码错误！\n";
        private static List<LoginModel> logins = new List<LoginModel>();
#endregion
        private List<LoginModel> getInfos()
        {
            List<LoginModel> xmlInfo = new List<LoginModel>();
            xmlInfo = (List<LoginModel>)ZdjXMLHelp.deserialize_from_xml(dir, xmlInfo.GetType());
            return xmlInfo;
        }
        private void setInfos(List<LoginModel> modes)
        {
            if (modes == null || modes.Count <= 0)
            {
                return;
            }
            ZdjXMLHelp.serialize_to_xml(dir, modes);
        }
        public UserController()
        {
            logins= getInfos();
        }
        private void setNowInfos(LoginModel mode)
        {
            if (mode == null)
            {
                return;
            }
            ZdjXMLHelp.serialize_to_xml(dirNow, mode);
        }
        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns></returns>
        public static LoginModel getNowInfos()
        {
            LoginModel mode = new LoginModel();
            mode = (LoginModel)ZdjXMLHelp.deserialize_from_xml(dirNow, mode.GetType());
            return mode;
        }
        /// <summary>
        /// 执行登陆
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public string login(LoginModel mode)
        {
            if (string.IsNullOrEmpty(mode.Password) || string.IsNullOrEmpty(mode.UserName))
            {
                return loginErro + notEmptyNameOrPaw;
            }
            mode = checkuser(mode);
            if (mode==null)
            {
                return loginErro + erroeNameOrPaw;
            }
            this.setNowInfos(mode);
            start(mode);
            return null;
        }

        private void start(LoginModel mode)
        {
            try
            {
                ProcessStartInfo process = new ProcessStartInfo();
                process.FileName = startUpPath + mode.RowName;
                process.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(process);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }
        private LoginModel checkuser(LoginModel mode)
        {
            LoginModel modelx = logins.Find(q => q.UserName.Equals(mode.UserName) && q.Password.Equals(mode.Password));
            if (modelx == null)
            {
                return null;
            }
            return modelx;

        }
        /// <summary>
        /// 新建用户
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string add(LoginModel obj)
        {
            obj.Guid= Guid.NewGuid().ToString("N");
            logins.Add(obj);
            return obj.Guid;
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public bool delete(string guid)
        {
          return  logins.Remove(logins.Find(q=>q.Guid.Equals(guid)));
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<LoginModel> getAll()
        {
            return logins;
        }
        /// <summary>
        /// 根据guid查询
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public LoginModel getGuid(string guid)
        {
            return logins.Find(q => q.Guid.Equals(guid));
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool upDate(LoginModel obj)
        {
            if (delete(obj.Guid))
            {
                logins.Add(obj);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 释放对象
        /// </summary>
        public void Dispose()
        {
            setInfos(logins);
        }
    }
}
